﻿// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;
using System.IO;

namespace Hack.Imageing
{
    public static class FrameRate
    {
        /// <summary>
        /// double interval = (double)Stopwatch.Frequency / 30.0;
        /// </summary>
        /// <param name="fps">30 = 30 frames /s</param>
        /// <returns></returns>
        public static double GetFrameRateFor(double fps)
        {
            return (double)System.Diagnostics.Stopwatch.Frequency / fps;
        }
    }
    public class GifImage
    {
        private System.Drawing.Image gifImage;
        private FrameDimension dimension;
        private int frameCount;
        private int currentFrame = -1;
        private bool reverse;
        private int step = 1;

        public GifImage(string path)
        {
            gifImage = System.Drawing.Image.FromFile(path); //initialize
            setUp();
        }
        public GifImage(Stream gifStream)
        {
            gifImage = System.Drawing.Image.FromStream(gifStream);
            setUp();
        }
        public GifImage(Stream gifStream, bool useEmbeddedColorManagement)
        {
            gifImage = System.Drawing.Image.FromStream(gifStream, useEmbeddedColorManagement);
            setUp();
        }
        public GifImage(Stream gifStream, bool useEmbeddedColorManagement, bool validateImageData)
        {
            gifImage = System.Drawing.Image.FromStream(gifStream, useEmbeddedColorManagement, validateImageData);
            setUp();
        }
        private void setUp()
        {
            dimension = new FrameDimension(gifImage.FrameDimensionsList[0]); //gets the GUID
            frameCount = gifImage.GetFrameCount(dimension); //total frames in the animation
        }

        public bool ReverseAtEnd //whether the gif should play backwards when it reaches the end
        {
            get { return reverse; }
            set { reverse = value; }
        }

        public System.Drawing.Image GetNextFrame()
        {

            currentFrame += step;

            //if the animation reaches a boundary...
            if (currentFrame >= frameCount || currentFrame < 1)
            {
                if (reverse)
                {
                    step *= -1; //...reverse the count
                    currentFrame += step; //apply it
                }
                else
                    currentFrame = 0; //...or start over
            }
            return GetFrame(currentFrame);
        }

        public System.Drawing.Image GetFrame(int index)
        {
            gifImage.SelectActiveFrame(dimension, index); //find the frame
            return (System.Drawing.Image)gifImage.Clone(); //return a copy of it
        }
    }
}
